<template>
  <div>
      <a-card :bordered="false">
    <a-spin :spinning="loading">
        <div class="config_content">
          <h3 :style="{ marginBottom: '0px',marginTop:'10px' }">教室设置</h3>
          <a-list :bordered="false">
            <a-list-item class="child_list" style>
              <span class="left_title">教室设置</span>
              <a-button @click="showChild">设置</a-button>
            </a-list-item>
          </a-list>
          <h3 :style="{ marginBottom: '0px',marginTop:'10px' }">点名设置</h3>
          <!-- <div style="padding:12px 30px 0;">开启后，点名时会隐藏对应的异常学员</div> -->
          <a-list :bordered="false">
            <a-list-item class="child_list" style>
              <span class="left_title">隐藏课时为0的学员</span>
              <a-switch v-model="value.class_hour_empty" @change="changeSwitch_stu('class_hour_empty',value.class_hour_empty)" />
            </a-list-item>
            <a-list-item class="child_list" style>
              <span class="left_title">隐藏按天课程时间到期的学员</span>
              <a-switch v-model="value.class_time_empty" @change="changeSwitch_stu('class_time_empty',value.class_time_empty)" />
            </a-list-item>
            <a-list-item class="child_list" style>
              <span class="left_title">隐藏按天课程时间未开始的学员</span>
              <a-switch v-model="value.class_time_no_begin" @change="changeSwitch_stu('class_time_no_begin',value.class_time_no_begin)" />
            </a-list-item>
            <a-list-item class="child_list" style>
              <span class="left_title">隐藏停课的学员</span>
              <a-switch v-model="value.suspend_class" @change="changeSwitch_stu('suspend_class',value.suspend_class)" />
            </a-list-item>
            <a-list-item class="child_list" style>
              <span class="left_title">学员默认到课状态为未到</span>
              <a-switch v-model="value.default_no_arrive_state" @change="changeSwitch_stu('default_no_arrive_state',value.default_no_arrive_state)" />
            </a-list-item>
          </a-list>
          <h3 :style="{ marginBottom: '0px',marginTop:'10px' }">课程设置</h3>
         <a-list :bordered="false">
           <!--  <a-list-item class="child_list" style>
          <span class="left_title">通用课程功能</span>
          <a-switch  />
          <span class="config_description">
            <a-icon type="info-circle" style="margin-right:5px;" />开启后，“课程/收费-课程管理”中显示“添加通用课程”按钮
          </span>
            </a-list-item>-->
            <a-list-item class="child_list" style>
              <span class="left_title">课程有效期</span>
              <a-switch v-model="courseTimeSwitch" @change="changeSwitch_course" />
              <span class="config_description">
                <a-icon type="info-circle" style="margin-right:5px;" />开启后，可以为“按课时”的课程添加有效期，超过有效期的课时将显示过期标识
              </span>
            </a-list-item>
          </a-list>
        </div>
        <classroom-components ref="classRoom"></classroom-components>
    </a-spin>
      </a-card>
  </div>
</template>

<script>
import classroomComponents from '@/components/classroomManage'
import { getCourseInfo, getHiddenStudentInfo, setHiddenStudentInfo, setCourseInfo } from '@/api/config'
export default {
  components: {
    classroomComponents
  },
  data() {
    return {
      loading: false,
      courseTimeSwitch: false,
      value: {
        class_hour_empty: false,
        class_time_empty: false,
        class_time_no_begin: false,
        suspend_class: false,
        default_no_arrive_state:false
      }
    }
  },
  created() {
    this.getData()
  },
  methods: {
    showChild() {
      this.$refs.classRoom.showModal()
    },
    getData() {
      this.loading = true
      getCourseInfo({
        school_id: this.$store.state.user.school,
        key: 'course_setting'
      })
        .then(res => {
          if(res.value&&JSON.stringify(res.value)!='{}'){
            this.courseTimeSwitch = res.value.valid_time
          }else{

          }
          // console.log(JSON.stringify(res.value))
          
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err.data.message || '加载失败')
        })

      getHiddenStudentInfo({
        school_id: this.$store.state.user.school,
        key: 'hide_student'
      })
        .then(res => {
          if (res.value && !(res.value instanceof Array)) {
            this.value = res.value
          } else {
            this.value = {
              class_hour_empty: false,
              class_time_empty: false,
              class_time_no_begin: false,
              suspend_class: false,
              default_no_arrive_state:false
            }
          }
        })
        .catch(err => {
          this.$message.error(err.data.message || '加载失败')
        })
        .finally(() => {
          this.loading = false
        })
    },
    changeSwitch_course() {
      let data = {
        school_id: this.$store.state.user.school,
        key: 'course_setting',
        value: {
          valid_time: false
        }
      }
      data.value.valid_time = this.courseTimeSwitch
      setCourseInfo(data)
        .then(res => {
          this.courseTimeSwitch = res.value.valid_time
        })
        .catch(err => {
          this.$message.error(err.data.message || '设置失败')
        })
    },
    changeSwitch_stu(key,val) {
      let data = {
        school_id: this.$store.state.user.school,
        key: 'hide_student',
      }
      data.value ={}
      data.value[key] = val
      this.loading = true
      setHiddenStudentInfo(data)
        .then(res => {
          this.getData()
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err.data.message || '设置失败')
        })
    }
  }
}
</script>
<style lang="less">
.child_list.ant-list-item {
  justify-content: flex-start !important;
  border-bottom: none;
}
</style>
<style lang="less" scoped>
.config_content {
  margin: 20px 30px;
}
.child_list {
  padding: 12px 30px;
  .left_title {
    display: inline-block;
    width: 250px;
    margin-right: 30px;
  }
  .config_description {
    margin-left: 30px;
    color: #909090;
  }
}
</style>